package com.resou.reader.bookstore.freezone.adapter;

import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.resou.reader.R;
import com.resou.reader.api.entry.FreezoneBean;
import com.resou.reader.bookdetail.BookDetailActivity;

import java.util.List;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:
 * CreateTime: 2019-03 -20
 **/
public class FreezoneAdapter extends BaseQuickAdapter<FreezoneBean, BaseViewHolder> {
    public FreezoneAdapter(int layoutResId, @Nullable List<FreezoneBean> data) {
        super(layoutResId, data);
    }

    public FreezoneAdapter(@Nullable List<FreezoneBean> data) {
        super(data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FreezoneBean item) {
        if (item.getCoverUrl() != null) {
            Glide.with(mContext).load(item.getCoverUrl()).dontTransform().into((ImageView) helper.getView(R.id.ivPic));
        }
        helper.setText(R.id.tvBookName, item.getNovelName());
        try {
            helper.setText(R.id.tvDes, item.getAuthorName() + "·" + setValue(item.getSerialStatus()) + "·" + calcValue(item.getTotalWord()));
        } catch (Exception e) {
            Log.i(TAG, "convert: " + e.getMessage());
        }
        helper.setText(R.id.tvType, item.getCategoryName());
        helper.setText(R.id.tvAbstract, item.getContent());
        helper.getView(R.id.llContent)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BookDetailActivity.startDetailActivity(mContext, item.getId());
                    }
                });
    }


    /**
     * 计算获取书籍的字数，超过或等于一万时，以万计数 否则
     *
     * @param value
     * @return
     */
    private String calcValue(int value) {
        if (value >= 10000) {
            return (int) (value / 10000) + "万字";
        } else {
            return (int) (value) + "字";
        }
    }

    private String setValue(int state) {
        String bookState = "";
        if (state == 1) {
            bookState = "连载";
        } else if (state == 2) {
            bookState = "暂停";
        } else if (state == 3) {
            bookState = "已完结";
        }
        return bookState;

    }
}
